import { getByteLen } from '@/utils/util'
import luckySheetStore from '@/store/luckySheet';
const luckyColor = [
  "#c1232b", "#27727b", "#fcce10", "#e87c25", "#b5c334",
  "#fe8463", "#9bca63", "#fad860", "#f3a43b", "#60c0dd",
  "#d7504b", "#c6e579", "#f4e001", "#f0805a", "#26c0c0",
  "#c12e34", "#e6b600", "#0098d9", "#2b821d", "#005eaa",
  "#339ca8", "#cda819", "#32a487", "#3fb1e3", "#6be6c1",
  "#626c91", "#a0a7e6", "#c4ebad", "#96dee8"
];
// 显示协同人员
export const multipleRangeShow=(id, name, r, c, value) => {
  const Store = luckySheetStore()
  let multipleIndex = 0
  const fullName=name;
  if(!Store.visibledatarow) {
    return false
  }
  let row = Store.visibledatarow[r],
      row_pre = r - 1 == -1 ? 0 : Store.visibledatarow[r-1],
      col = Store.visibledatacolumn[c],
      col_pre = c - 1 == -1 ? 0 : Store.visibledatacolumn[c-1];
  let margeset = mergeborer(Store.flowdata, r, c);
  if(!!margeset){
    row = margeset.row[1];
    row_pre = margeset.row[0];

    col = margeset.column[1];
    col_pre = margeset.column[0];
  }

  // 超出16个字符就显示...
  if (getByteLen(name)>16) {
    name=getByteLen(name, 16)+"...";
  }
  // 如果正在编辑，就显示“正在输入”
  if (value==='enterEdit') {
    name+="正在输入";
  }

  // *处理光标在靠左或者靠上顶着的时候，光标显示不全的问题
  let rightPos = -(((name.length) * 12 + 20) - (col-col_pre-1)) / 2 // 用户信息right位置
  let bottomPos = row - row_pre - 1 // 用户信息bottom位置
  if (col_pre<=0) {
    col_pre+=1
    rightPos = rightPos * 2
  }
  if (row_pre<=0) {
    row_pre+=1
    bottomPos = -bottomPos-3
  }
  if($("#luckysheet-multipleRange-show-"+id).length>0) {
    $("#luckysheet-multipleRange-show-"+id).css({ "position": "absolute", "left": col_pre-1, "width": col-col_pre-1, "top": row_pre-1, "height": row - row_pre - 1 });
    $("#luckysheet-multipleRange-show-"+id+" .username").css({bottomPos})
    $("#luckysheet-multipleRange-show-"+id+" .username").text(name);
    $("#luckysheet-multipleRange-show-"+id+" .username").show();
  }else {
    let itemHtml=`<div
      id="luckysheet-multipleRange-show-${id}"
      class="luckysheet-multipleRange-show"
      data-color="${luckyColor[multipleIndex]}"
      title="${fullName}"
      style="position: absolute;left: ${col_pre-1}px;width: ${col-col_pre-1}px;top: ${row_pre-1}px;height: ${row - row_pre - 1}px;border: 1px solid ${luckyColor[multipleIndex]};z-index: 15;">
      <div class="username" style="height: 21px;line-height:21px;width: max-content;position: absolute;bottom: ${bottomPos}px;left: -1px;background-color: ${luckyColor[multipleIndex]};color:#ffffff;padding:0 10px;">
        ${name}
      </div>
      <div style="width: 100%;height: 100%;position: absolute;top: 0;right: 0;bottom: 0;left: 0;opacity: 0.03;background-color: ${luckyColor[multipleIndex]}">
      </div>
    </div>`;
    // 正在输入
    $(itemHtml).appendTo($("#luckysheet-cell-main #luckysheet-multipleRange-show"));
    multipleIndex++;
  }
}
// 隐藏协同人员
export const muitipleRangeHide = (id) => {
  if($("#luckysheet-multipleRange-show-"+id).length>0) {
    $("#luckysheet-multipleRange-show-"+id).remove()
  }
}
// 刷新协同人员显示
export const multipleRangeRefresh = (id, name, r, c,order)=>{
  const Store = luckySheetStore()
  const fullName=name;
  if(!Store.visibledatarow) {
    return false
  }
  let row = Store.visibledatarow[r],
      row_pre = r - 1 == -1 ? 0 : Store.visibledatarow[r-1],
      col = Store.visibledatacolumn[c],
      col_pre = c - 1 == -1 ? 0 : Store.visibledatacolumn[c-1];
  let margeset = mergeborer(Store.flowdata, r, c);
  if(!!margeset){
    row = margeset.row[1];
    row_pre = margeset.row[0];

    col = margeset.column[1];
    col_pre = margeset.column[0];
  }

  // // 超出16个字符就显示...
  // if (getByteLen(name)>16) {
  //   name=getByteLen(name, 16)+"...";
  // }
  // // 如果正在编辑，就显示“正在输入”
  // if (value==='enterEdit') {
  //   name+="正在输入";
  // }

  // *处理光标在靠左或者靠上顶着的时候，光标显示不全的问题
  let rightPos = -(((name.length) * 12 + 20) - (col-col_pre-1)) / 2 // 用户信息right位置
  let bottomPos = row - row_pre - 1 // 用户信息bottom位置
  if (col_pre<=0) {
    col_pre+=1
    rightPos = rightPos * 2
  }
  if (row_pre<=0) {
    row_pre+=1
    bottomPos = -bottomPos-3
  }
  $("#luckysheet-multipleRange-show-"+id).css({ "position": "absolute", "left": col_pre-1, "width": col - col_pre - 1, "top": row_pre-1, "height": row - row_pre - 1 });
  $("#luckysheet-multipleRange-show-"+id+" .username").css({bottomPos})
  $("#luckysheet-multipleRange-show-"+id+" .username").show();
}
// 合并单元格
export const mergeborer = (d, row_index, col_index) => {
  const Store = luckySheetStore()
  if (d==null||d[row_index]==null) {
    console.warn("Merge info is null", row_index, col_index);
    return null;
  }
  let value=d[row_index][col_index];
  if (typeof value == "object" && value && ("mc" in value)) {
    let margeMaindata=value["mc"];
    if (margeMaindata==null) {
      console.warn("Merge info is null", row_index, col_index);
      return null;
    }
    col_index=margeMaindata.c;
    row_index=margeMaindata.r;
    if (d[row_index][col_index]==null) {
      console.warn("Main merge Cell info is null", row_index, col_index);
      return null;
    }
    let col_rs=d[row_index][col_index].mc.cs;
    let row_rs=d[row_index][col_index].mc.rs;
    let margeMain=d[row_index][col_index].mc;
    let start_r, end_r, row, row_pre;
    for (let r=row_index; r<margeMain.rs+row_index; r++) {
      if (r==0) {
        start_r=- 1;
      }
      else {
        start_r=Store.visibledatarow[r-1]-1;
      }
      end_r=Store.visibledatarow[r];
      if (row_pre==null) {
        row_pre=start_r;
        row=end_r;
      }
      else {
        row+=end_r-start_r-1;
      }
    }
    let start_c, end_c, col, col_pre;
    for (let c=col_index; c<margeMain.cs+col_index; c++) {
      if (c==0) {
        start_c=0;
      }
      else {
        start_c=Store.visibledatacolumn[c-1];
      }
      end_c=Store.visibledatacolumn[c];
      if (col_pre==null) {
        col_pre=start_c;
        col=end_c;
      }
      else {
        col+=end_c-start_c;
      }
    }
    return {
      "row": [row_pre, row, row_index, row_index+row_rs-1],
      "column": [col_pre, col, col_index, col_index+col_rs-1]
    };
  }else {
    return null;
  }
}